package com.controller;

import com.model.FlightModel;
import com.services.IFlightServices;
import com.services.impl.FlightServicesImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/Flight/save")
public class FlightServletController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Map<String, Object> result = new HashMap<>();

        try {
            // 获取表单参数,但使用新的字段名
            String fidStr = request.getParameter("fid");
            String fname = request.getParameter("fname");
            String finfo = request.getParameter("finfo");
            String fimg = request.getParameter("fimg");
            String frentStr = request.getParameter("frent");
            String fstatusStr = request.getParameter("fstatus");
            String ffrom = request.getParameter("ffrom");
            String fto = request.getParameter("fto");
            String fstart = request.getParameter("fstart");
            String fend = request.getParameter("fend");

            // 参数验证
            if (fname == null || fname.trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "航班名称不能为空");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            if (finfo == null || finfo.trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "航班信息不能为空");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            if (fimg == null || fimg.trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "航班图片不能为空");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            // 解析数字参数
            double frent;
            try {
                frent = Double.parseDouble(frentStr);
                if (frent < 0) {
                    result.put("code", 400);
                    result.put("msg", "票价不能小于0");
                    response.getWriter().write(JSONObject.fromObject(result).toString());
                    return;
                }
            } catch (NumberFormatException e) {
                result.put("code", 400);
                result.put("msg", "票价格式不正确");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            int fstatus = fstatusStr != null ? Integer.parseInt(fstatusStr) : 1;

            // 创建 FlightModel 对象
            FlightModel flightModel = new FlightModel();
            if (fidStr != null && !fidStr.trim().isEmpty()) {
                flightModel.setFid(Integer.parseInt(fidStr));
            }
            flightModel.setFname(fname);
            flightModel.setFinfo(finfo);
            flightModel.setFimg(fimg);
            flightModel.setFrent(frent);
            flightModel.setfStatus(fstatus);
            flightModel.setFfrom(ffrom);
            flightModel.setFto(fto);
            flightModel.setFstart(fstart);
            flightModel.setFend(fend);

            // 调用服务添加或更新
            IFlightServices flightServices = new FlightServicesImpl();
            if (flightModel.getFid() != null) {
                flightServices.updateFlight(flightModel);
                result.put("msg", "更新成功");
            } else {
                flightServices.addFlight(flightModel);
                result.put("msg", "添加成功");
            }

            result.put("code", 0);

        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败：" + e.getMessage());
        }

        response.getWriter().write(JSONObject.fromObject(result).toString());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}